<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <%@ include file="/common/pre/header.jsp" %>
    <script src="${ctx}/statics/js/backend/backend.js"></script>
</head>
<body>
<%@ include file="/common/backend/searchBar.jsp" %>
<!--End Header End-->
<div class="i_bg bg_color">
    <!--Begin 用户中心 Begin -->
    <div class="m_content">
        <%@ include file="/common/backend/leftBar.jsp" %>
        <div class="m_right" id="content">
             <div class="mem_tit">
                <c:choose>
                    <c:when test="${empty product.id || product.id==0}">
                        添加商品
                    </c:when>
                    <c:otherwise>
                        修改商品
                    </c:otherwise>
                </c:choose>
            </div>
            
            <br>
            <form action="${ctx}/productAddServlet" method="post" enctype="multipart/form-data" id="productAdd" onsubmit="return checkProduct();">
            <table border="0" class="add_tab" style="width:930px;" cellspacing="0" cellpadding="0">
                <tr>
                    <td width="135" align="right">一级分类</td>
                    <td colspan="3" style="font-family:'宋体';">
                        <select name="categoryLevel1Id" style="background-color:#f6f6f6;" id="productCategoryLevel1"
                                onchange="cate2();">
                            <option value="" selected="selected">请选择...</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td width="135" align="right">二级分类</td>
                    <td>
                        <select name="categoryLevel2Id" style="background-color:#f6f6f6;"
                                id="productCategoryLevel2"
                                onchange="cate3()">
                            <option value="0" selected="selected">请选择...</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td width="135" align="right">三级分类</td>
                    <td>
                        <select name="categoryLevel3Id" style="background-color:#f6f6f6;"
                                id="productCategoryLevel3">
                            <option value="0" selected="selected">请选择...</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td width="135" align="right">商品名称</td>
                    <td>
                        <input type="text" value="${product.name}" class="add_ipt" name="name" id="name"/>（必填）
                        <input type="hidden" name="id" value="${product.id}" id="id">
                    </td>
                </tr>
                <tr>
                    <td width="135" align="right">商品图片</td>
                    <td>
                        <c:if test="${product.fileName!=null && product.fileName!=''}">
                            <img src="${ctx}/files/${product.fileName}" width="50" height="50"/>
                        </c:if>
                        <input type="file" class="text" name="photo" /><span></span>
                    </td>
                </tr>
                <tr>
                    <td width="135" align="right">单价</td>
                    <td>
                        <input type="text" value="${product.price}" class="add_ipt" name="price" id="price"/>
                    </td>
                </tr>
                <tr>
                    <td width="135" align="right">库存</td>
                    <td>
                        <input type="text" value="${product.stock}" class="add_ipt" name="stock" id="stock"/>
                    </td>
                </tr>
                <tr>
                    <td width="135" align="right">描述</td>
                    <td>
                        <textarea name="description">${product.description}</textarea>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <c:choose>
                            <c:when test="${empty product.id}">
                                <input type="submit" value="商品上架" class="s_btn">
                            </c:when>
                            <c:otherwise>
                                <input type="submit" value="确定修改" class="s_btn">
                            </c:otherwise>
                        </c:choose>
                    </td>
                </tr>
            </table>
            </form>
        </div>
    </div>
    <%@ include file="/common/pre/footer.jsp" %>
</div>
</body>
<script>
    cate1();
    function cate1() {
        $.post("${ctx}/cateServlet",{"pid":0,"type":1},function (data) {
            // alert(data);
            for (let i = 0; i < data.length ; i++) {
                var ele =data[i];
                var option1 = $("<option value='"+ele.id+"'>"+ele.name+"</option>");
                var cate1=$("#productCategoryLevel1");
                cate1.prepend(option1);
            }
        },"json");
    }

    function cate2() {
        let $cate1 = $("#productCategoryLevel1").val();
        $.post("${ctx}/cateServlet",{"pid":$cate1,"type":2},function (data) {
            // alert(data);
            var cate2=$("#productCategoryLevel2");
            cate2.empty();
            let $option = $("<option value=\"0\">请选择</option>");
            cate2.prepend($option);
            for (let i = 0; i < data.length ; i++) {
                var ele =data[i];
                var option2 = $("<option value='"+ele.id+"'>"+ele.name+"</option>");
                cate2.prepend(option2);
            }
        },"json");
    }

    function cate3() {
        let $cate1 = $("#productCategoryLevel2").val();
        $.post("${ctx}/cateServlet",{"pid":$cate1,"type":3},function (data) {
            var cate2=$("#productCategoryLevel3");
            cate2.empty();
            let $option = $("<option value=\"0\">请选择</option>");
            cate2.prepend($option);
            for (let i = 0; i < data.length ; i++) {
                var ele =data[i];
                var option2 = $("<option value='"+ele.id+"'>"+ele.name+"</option>");
                cate2.prepend(option2);
            }
        },"json");
    }

</script>
</html>


